package com.santiao.publics.ad.ads;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.AdSlot.Builder;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdNative.FullScreenVideoAdListener;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd.FullScreenVideoAdInteractionListener;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationFullScreenManager;
import com.santiao.publics.config.Const;
import com.umeng.umcrash.UMCrash;


/**
 * 融合demo，【插全（半）屏广告】使用示例。更多功能参考接入文档。
 *
 * 注意：每次加载的广告，只能展示一次
 *
 * 接入步骤：
 * 1、创建AdSlot对象
 * 2、创建TTAdNative对象
 * 3、创建加载、展示监听器
 * 4、加载广告
 * 5、加载成功后，展示广告
 * 6、在onDestroy中销毁广告
 */
public   class MediationInterstitialFullService     {


    //是否显示成功

    public static  String ifInterstitialFullAdShow = "1"; //0 显示成功 1 初始化中 2加载失败

    //插图方法是否调用
    public   boolean ifShowInterstitialFullAd = false;
    public   boolean ifOnFullScreenVideoCached = false;
    //广告是否加载成功
    public    boolean ifOnFullScreenVideoAdLoad = false;
    public   String mMediaId; // 融合广告位
    public   TTFullScreenVideoAd mTTFullScreenVideoAd; // 插全屏广告对象
    private   FullScreenVideoAdListener mFullScreenVideoListener; // 广告加载监听器
    private   FullScreenVideoAdInteractionListener mFullScreenVideoAdInteractionListener; // 广告展示监听器

    public   MediationFullScreenManager manager;

    public  String ecpm = "";
    public  String slotId = "";

    public  String adStyle = "2";

    public  int showInterstitialFullAdCount=0;

    //显示成功次数
    public  int interstitialFullOnAdShow=0;

    public     void  intiDate(Context context , String mMediaIds){
        showInterstitialFullAdCount = 0;
        interstitialFullOnAdShow=0;
        ifInterstitialFullAdShow = "1";
        String simpleName = "";
        if(context!=null && context.getClass()!=null){
            simpleName = context.getClass().getSimpleName();
        }
        Log.d(Const.TAG,"MediationInterstitialFullService-----InterstitialFull-------intiDate:"+simpleName+"------------"+showInterstitialFullAdCount);
//        mFeedContainer = mFeedContainers;
        mMediaId  = mMediaIds;
//        adRequestService = new AdRequestService();
        ifShowInterstitialFullAd = false;
        ifOnFullScreenVideoAdLoad = false;
        ifOnFullScreenVideoCached = false;


        loadInterstitialFullAd(context);
    }
    public  MediationFullScreenManager getManager(){
        return manager;
    }
//
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        this.setContentView(R.layout.mediation_activity_interstitial_full);
//
//        // 聚合广告位（在GroMore平台的广告位，注意不是adn的代码位）
//        this.mMediaId = getResources().getString(R.string.full_media_id);
//        TextView tvMediationId = (TextView)this.findViewById(R.id.tv_media_id);
//        tvMediationId.setText(getString(R.string.ad_mediation_id, this.mMediaId));
//
//        // 加载广告
//        this.findViewById(R.id.bt_load).setOnClickListener((OnClickListener)(new OnClickListener() {
//            public final void onClick(View it) {
//                loadInterstitialFullAd();
//            }
//        }));
//
//        // 展示广告
//        this.findViewById(R.id.bt_show).setOnClickListener((OnClickListener)(new OnClickListener() {
//            public final void onClick(View it) {
//                showInterstitialFullAd();
//            }
//        }));
//    }

    public    void loadInterstitialFullAd(Context context) {
        try {
            /** 1、创建AdSlot对象 */
            AdSlot adslot = new Builder()
                    .setCodeId(mMediaId)
                    .setOrientation(TTAdConstant.ORIENTATION_VERTICAL)
                    .build();

            /** 2、创建TTAdNative对象 */
            TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(context);

            /** 3、创建加载、展示监听器 */
            initListeners();

            /** 4、加载广告 */
            adNativeLoader.loadFullScreenVideoAd(adslot, mFullScreenVideoListener);
        }catch(Exception e){
            if(Const.IF_UM) {
                UMCrash.generateCustomLog(e, "MediationFullScreenManager");
            }
            e.printStackTrace();
        }finally {

            AdRequestService adRequestService = new AdRequestService();
            adRequestService.initAdRequest("2",adStyle,ecpm,mMediaId,slotId,null);
            adRequestService.reportRequest();
        }

    }

    // 在加载成功后展示广告
    public    void showInterstitialFullAd(Activity  activity) {
        ++showInterstitialFullAdCount;



        String SimpleName = "";
        if(activity!=null && activity.getClass()!=null){
            SimpleName = activity.getClass().getSimpleName();
        }


        Log.d(Const.TAG,"MediationFeedService-----showInterstitialFullAd-------intiDate:"+SimpleName+"----------------"+showInterstitialFullAdCount);


        ifShowInterstitialFullAd = true;
        if (mTTFullScreenVideoAd == null) {
            Log.d(Const.TAG, "请先加载广告或等待广告加载完毕后再调用show方法");
            return;
        }
        /** 5、设置展示监听器，展示广告 */
        mTTFullScreenVideoAd.setFullScreenVideoAdInteractionListener( mFullScreenVideoAdInteractionListener);
        mTTFullScreenVideoAd.showFullScreenVideoAd(activity);
    }

    private     void initListeners() {
        // 广告加载监听器
         mFullScreenVideoListener = new FullScreenVideoAdListener() {
            public void onError(int code, String message) {
                ifInterstitialFullAdShow ="2";
                Log.d(Const.TAG, "InterstitialFull onError code = " + code + " msg = " + message);
            }

            public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ad) {
                ifOnFullScreenVideoAdLoad = true;
                Log.d(Const.TAG, "InterstitialFull onFullScreenVideoLoaded");
                mTTFullScreenVideoAd = ad;
                manager = mTTFullScreenVideoAd.getMediationManager();

            }

            public void onFullScreenVideoCached() {
                Log.d(Const.TAG, "InterstitialFull onFullScreenVideoCached1");
            }

            public void onFullScreenVideoCached(TTFullScreenVideoAd ad) {
                Log.d(Const.TAG, "InterstitialFull onFullScreenVideoCached2");
                mTTFullScreenVideoAd = ad;
                ifOnFullScreenVideoCached=true;
            }
        };
        // 广告展示监听器
         mFullScreenVideoAdInteractionListener = new FullScreenVideoAdInteractionListener() {
            public void onAdShow() {
                ++interstitialFullOnAdShow;
                ifInterstitialFullAdShow ="0";
                Log.d(Const.TAG, "InterstitialFull onAdShow");

                if(manager!=null){
                    ecpm = manager.getShowEcpm().getEcpm();
                    slotId = manager.getShowEcpm().getSlotId();
                }
                AdRequestService adRequestService = new AdRequestService();
                adRequestService.initAdRequest("0",adStyle,ecpm,mMediaId,slotId,null);
                adRequestService.reportRequest();
            }

            public void onAdVideoBarClick() {
                Log.d(Const.TAG, "InterstitialFull onAdVideoBarClick");

                if(manager!=null){
                    ecpm = manager.getShowEcpm().getEcpm();
                    slotId = manager.getShowEcpm().getSlotId();
                }
                AdRequestService adRequestService = new AdRequestService();
                adRequestService.initAdRequest("1",adStyle,ecpm,mMediaId,slotId,null);
                adRequestService.reportRequest();
            }

            public void onAdClose() {
                Log.d(Const.TAG, "InterstitialFull onAdClose");
            }

            public void onVideoComplete() {
                Log.d(Const.TAG, "InterstitialFull onVideoComplete");
            }

            public void onSkippedVideo() {
                Log.d(Const.TAG, "InterstitialFull onSkippedVideo");
            }
        };
    }

//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        /** 6、在onDestroy中销毁广告 */
//        if (mTTFullScreenVideoAd != null && mTTFullScreenVideoAd.getMediationManager() != null) {
//            mTTFullScreenVideoAd.getMediationManager().destroy();
//        }
//    }
}
